import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Menu, MenuPermission, SysroleMenu } from '../domain/menu';

@Injectable({
  providedIn: 'root'
})
export class MenuService {
  host:string = "http://localhost:8091/api";

  constructor(private httpClient: HttpClient) { }

  public getTreeByParentId(parentId: string){
    return this.httpClient.get<any>(this.host+"/menus/getTreeByParentId",{ params:{parentId:parentId}})
  }

  public getMyTree(){
    return this.httpClient.get<any>(this.host+"/menus/getMyTree")
  }

  public saveBatch(levels: Menu[]){
    return this.httpClient.post(this.host+"/menus/batch",levels);
  }

  public save(level: Menu){
    if(level.id === undefined){
      return this.httpClient.post<Menu>(this.host+"/menus",level);
    }else{
      return this.httpClient.patch<Menu>(this.host+"/menus/" + level.id,level);
    }
  }

  // public saveRelations(levelRelations: LevelRelation[]){
  //   return this.httpClient.post(this.host+"/levels/relations/batch",levelRelations);
  // }

  public getModelName(){
    return this.httpClient.get<any>(this.host+"/menus/getModelName");
  }

  deleteBatch(ids$: any) {
    return this.httpClient.delete(this.host+"/menus/batch",{body:ids$});
  }

  addMenuPermission(menuPermission: MenuPermission){
    return this.httpClient.post(this.host + "/menus/permissions", menuPermission);
  }

  removeMenuPermission(menuPermission: MenuPermission){
    return this.httpClient.delete(this.host + "/menus/permissions", {body:menuPermission});
  }

  getSysroles(menuId: string){
    return this.httpClient.get<SysroleMenu[]>(this.host + "/menus/sysroles",{params:{menuId: menuId}})
  }
}
